#coding=utf-8
import webbrowser
from base_client import base_client

class client(base_client):
    def __init__(self, config=None ):
        if None==config:
            config = { 
                      "blog_user":"",
                      "blog_password":"",
                      "microblog_user":"",
                      "microblog_password":"",
             }
        base_client.__init__( self, config )

    def __blog_login(self):
        pass

    def __microblog_login(self):
        self.browser.open( 'http://mini.t.qq.com/mblogin_quick.htm' )
        self.browser.select_form( name="loginform" )
        self.browser["u"] = self.var_microblog_user.get()
        self.browser["p"] = self.var_microblog_password.get()
        self.browser["verifycode"] = self.var_verifycode.get()
        self.browser.submit()

    def post_blog(self, title, content ):
        self.btn_blog_state.config( bg="yellow" )
        try:
            self.__blog_login()
            self.browser.submit()
        except:
            self.btn_blog_state.config( bg="red" )        
        else:
            self.btn_blog_state.config( bg="blue" )

    # not ready
    def post_microblog(self, content):
        self.btn_microblog_state.config( bg="yellow" )
        try:
            self.__microblog_login()
            self.browser.submit()
        except:
            self.btn_microblog_state.config( bg="red" )        
        else:
            self.btn_microblog_state.config( bg="blue" )
    
    def display_blog_option(self, father):
        base_client.display_blog_option( self, father, "QQ", self.call_browser )

    def display_microblog_option(self, father):
        base_client.display_microblog_option( self, father, "QQ", self.call_browser )

    def call_browser(self):
        webbrowser.open_new_tab('http://www.qq.com')
    
    def update_config(self):
        if "var_blog_user" in dir(self):
            self.config["blog_user"] = self.var_blog_user.get()
            self.config["blog_password"] = self.var_blog_password.get()
        if "var_microblog_user" in dir(self):
            self.config["microblog_user"] = self.var_microblog_user.get()
            self.config["microblog_password"] = self.var_microblog_password.get()
        